﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class usergroup_privilege_assign : System.Web.UI.Page
    {
        private Guid _selectGroupId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _selectGroupId = new Guid(Request.QueryString["id"]);

                ICoreService service = ServiceLocator.Get<ICoreService>();
                PagedList<Privilege> privileges = service.GetPrivileges(string.Empty, 0, 999);
                _ctlListPrivilege.DataSource = privileges;
                _ctlListPrivilege.DataBind();
            }
        }

        protected void _ctlBtnAssign_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            foreach (RepeaterItem item in _ctlListPrivilege.Items)
            {
                CheckBox checkBox = (CheckBox)item.FindControl("_ctlCheck");
                if (checkBox != null && checkBox.Checked)
                {
                    HiddenField hiddenField = (HiddenField)item.FindControl("_ctlHiddenId");
                    Guid userId = new Guid(hiddenField.Value);
                    User user = service.GetUser(userId);
                    service.AssignUserToUserGroup(userId, _selectGroupId);
                }
            }
            Response.Redirect(_ctlLinkBack.NavigateUrl);
        }
    }
}